package com.dly.blog.controller;

import cn.hutool.http.server.HttpServerResponse;
import com.dly.blog.annotation.IgnoreAccess;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.netty.http.client.HttpClient;
import reactor.netty.transport.ProxyProvider;

import java.net.InetSocketAddress;
import java.time.Duration;

/**
 * @Author: DaiLiYu
 * @Date: 2023/7/24 16:05
 */
@RestController
@RequestMapping("test")
@IgnoreAccess
public class GptController {

    private static String openKey = "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";

    @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> gptFlux(){
        WebClient.Builder builder = WebClient.builder();
        return builder.baseUrl("https://chatgpt1.nextweb.fun/api/proxy/v1")
                .defaultHeader("Authorization", "Bearer " + openKey)
                .defaultHeader("Content-Type", "application/json")
                .build()
                .post()
                .uri("/chat/completions")
                .bodyValue("""
                                {
                                     "model": "gpt-3.5-turbo",
                                     "messages": [
                                        {"role": "user", "content": "在干嘛!"},
                                        {"role": "assistant","content": "作为一名AI语言模型，我在这里等待人类用户的提问和交流，帮助他们解决问题、获取信息或提供娱乐。请问有什么可以帮到您的吗？"},
                                        {"role":"user", "content":"我刚刚说了什么？"}],
                                     "temperature": 0.7
                                }
                        """)
                .retrieve()
                .bodyToFlux(String.class)
                .timeout(Duration.ofSeconds(60L));
    }
}
